package com.sram.controller.upload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Iterator;

/**
 * Created by Administrator on 2017/5/9.
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
    @RequestMapping(value="/upload")
    public  String upload() throws IOException {

        return "upload/upload";
    }
    @RequestMapping(value="/upload33")
    public  String upload33() throws IOException {

        return "upload/upload2";
    }
    @RequestMapping(value="/toUpload")
    public  String toUpload(HttpServletRequest request) throws IOException {

        long  startTime=System.currentTimeMillis();
        //将当前上下文初始化给  CommonsMutipartResolver （多部分解析器）
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if(multipartResolver.isMultipart(request))
        {
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
            //获取multiRequest 中所有的文件名
            Iterator iter=multiRequest.getFileNames();

            while(iter.hasNext())
            {
                //一次遍历所有文件
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                InputStream is=file.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String line;
                // 读取每行数据
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                if(file!=null)
                {
                    String path="E:/杜浩"+file.getOriginalFilename();
                    //上传到本地
                    file.transferTo(new File(path));
                }

            }

        }
        long  endTime=System.currentTimeMillis();
        System.out.println("方法三的运行时间："+String.valueOf(endTime-startTime)+"ms");
        return "upload/upload";
    }
    @RequestMapping(value="/toUpload2")
    public @ResponseBody String toUpload2(HttpServletRequest request) throws IOException {
        String meg ="";
        long  startTime=System.currentTimeMillis();
        //将当前上下文初始化给  CommonsMutipartResolver （多部分解析器）
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if(multipartResolver.isMultipart(request))
        {
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
            //获取multiRequest 中所有的文件名
            Iterator iter=multiRequest.getFileNames();

            while(iter.hasNext())
            {
                //一次遍历所有文件
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                String fileName = file.getOriginalFilename();
                String[] split1 = fileName.split("\\.");
                if(!split1[split1.length-1].equals("txt")){
                    meg="不是text文件";
                }else {
                    InputStream is=file.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String line;
                    // 读取每行数据
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                    if(file!=null)
                    {
                        String path="E:/杜浩"+file.getOriginalFilename();
                        //上传到本地
                        file.transferTo(new File(path));
                    }
                    meg = "ok";
                }
            }

        }
        long  endTime=System.currentTimeMillis();
        System.out.println("方法三的运行时间："+String.valueOf(endTime-startTime)+"ms");
        return meg;
    }
    @RequestMapping(value="/toUpload34")
    public @ResponseBody String toUpload34(HttpServletRequest request) throws IOException, InterruptedException {
        String meg ="";
        long  startTime=System.currentTimeMillis();
        //将当前上下文初始化给  CommonsMutipartResolver （多部分解析器）
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        Thread.sleep(100000);
        if(multipartResolver.isMultipart(request))
        {
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
            //获取multiRequest 中所有的文件名
            Iterator iter=multiRequest.getFileNames();

            while(iter.hasNext())
            {
                //一次遍历所有文件
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                String fileName = file.getOriginalFilename();
                String[] split1 = fileName.split("\\.");
                if(!split1[split1.length-1].equals("sql")){
                    meg="不是sql文件";
                }else {
                    InputStream is=file.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String line;
                    // 读取每行数据
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                    if(file!=null)
                    {
                        String path="E:/杜浩"+file.getOriginalFilename();
                        //上传到本地xxxx
                        file.transferTo(new File(path));
                    }
                    meg = "ok";
                }
            }

        }
        long  endTime=System.currentTimeMillis();
        System.out.println("方法三的运行时间："+String.valueOf(endTime-startTime)+"ms");
        return meg;
    }
}
